package xyz.thoughtset.viewer.executor.blocks.executor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.image.Image;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.model.Model;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import xyz.thoughtset.viewer.common.ai.model.entity.ModelParam;
import xyz.thoughtset.viewer.common.exc.exceptions.ExecException;
import xyz.thoughtset.viewer.modules.step.entity.ExecAIBody;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockBodyEle;
import xyz.thoughtset.viewer.modules.step.entity.block.BlockInfo;
import xyz.thoughtset.viewer.modules.step.entity.block.EleParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class ExecAIBlockExecutor extends AbstractAISupportBlockExecutor<ExecAIBody> {


    @Override
    protected Object doQuery(BlockInfo block, ExecAIBody body, Map<String, Object> params, ExpressionParser parser, StandardEvaluationContext context) throws ExecException {
//        HashMap<String, Object> resultMaps = new HashMap<>(params);
//        List<EleParam> eleParams = body.getDataParams();
//        if (Objects.nonNull(eleParams)){
//            for (EleParam blockParam : eleParams){
//                Object value = "";
//                if(StringUtils.hasLength(blockParam.getDataExp())){
//                    String exp =blockParam.getDataExp();
//                    exp = blockParam.getDataExp().startsWith("#")?exp:"#"+exp;
//                    value = parser.parseExpression(exp).getValue(context);
//                }
//                String key = blockParam.getParamId();
//                if (StringUtils.hasText(key) && !ObjectUtils.isEmpty(value)){
//                    if (!(value instanceof String)){
//                        try {
//                            value = objectMapper.writeValueAsString(value);
//                        } catch (Exception e) {
//                            log.error("objectMapper error", value);
//                            e.printStackTrace();
//                            value = "";
//                        }
//                    }
//                    resultMaps.put(key, value);
//                }
//            }
//        }
        HashMap resultMaps = filterDataAsMapForPrompt(body, params, parser, context);
        ModelParam modelParam = loadModelParam(body);
        String userPrompt = fillAndRenderPromptToStr(body.getUserMsg(), parser, context, resultMaps);
//        String systemPrompt = fillPrompt(modelParam.getSystemPrompt(), parser, context);
        Model aiModel = modelFactory.buildModel(modelParam);
        Object result = null;
        if (aiModel instanceof ImageModel){
            result = ((ImageModel) aiModel).call(
                    new ImagePrompt(userPrompt)
            ).getResult().getOutput();
        }
//        aiModel.
//        if (StringUtils.hasText(systemPrompt)){
//            builder.defaultSystem(systemPrompt);
//        }
//        ChatClient client = builder.build();
        return result;
    }
}
